package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Product;
import com.itheima.utils.BeanUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


@WebServlet("/product")
public class ProductServlet extends BaseServlet {

    /**
     * 查看页面，分页并且实现模糊查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum=1;
        int pageSize=5;
        if (StringUtils.isNotBlank(request.getParameter("pageNum"))){
            pageNum=Integer.parseInt(request.getParameter("pageNum"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))){
            pageSize=Integer.parseInt(request.getParameter("pageSize"));
        }

        //获取Value值，即为前端页面传到模糊查询数据
        String value = request.getParameter("value");
        Product product=new Product();
        product.setPageNum(pageNum);
        product.setPageSize(pageSize);
        product.setValue(value);
        //封装请求转发
        PageInfo all = productService.findAll(product);
        request.setAttribute("page",all);
        request.getRequestDispatcher("/WEB-INF/pages/product/product-list.jsp").forward(request,response);
    }

    /**
     * 前往添加页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       /* List<Product> allProduct = productService.findAll();
        ObjectMapper objectMapper=new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(allProduct).toString());*/

        List<String> city = productService.findCity();
        request.setAttribute("city",city);
        request.getRequestDispatcher("/WEB-INF/pages/product/product-add.jsp").forward(request,response);
    }

    /**
     * 进行添加
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Product product = BeanUtil.fillBean(request, Product.class);
        productService.save(product);
        response.sendRedirect(request.getContextPath()+"/product?operation=list");
    }

    /**
     * 根据id进行删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String ids = request.getParameter("ids");
        String[] split = ids.split(",");
        System.out.println(split);
        productService.delete(split);
        response.sendRedirect(request.getContextPath()+"/product?operation=list");
    }

    /**
     *去修改页面，数据回显
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void toEdit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Product byId = productService.findById(id);
        request.setAttribute("byId",byId);
        List<String> city = productService.findCity();
        request.setAttribute("city",city);
        request.getRequestDispatcher("/WEB-INF/pages/product/product-update.jsp").forward(request,response);
    }

    /**
     *获取修改的数据进行修改
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void edit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        Product product = BeanUtil.fillBean(request, Product.class);
        productService.update(product);
        response.sendRedirect(request.getContextPath()+"/product?operation=list");
    }

    /**
     * 根据传入id和状态值，进行修改
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateStatus(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        productService.updateStatus(id);
        response.sendRedirect(request.getContextPath()+"/product?operation=list");
    }

}
